<?php
/**
 * Created by PhpStorm.
 * User: whwyy
 * Date: 2018/6/27 0027
 * Time: 18:10
 */

namespace Database;


class Relate
{
	
	/** @var array */
	public static $values = [];
	
	/** @var ActiveRecord */
	public $model;
	
	public $localKey = '';
	
	/**
	 * @param $value
	 * @param null $model
	 */
	public function append($value, &$model = NULL)
	{
		array_push(static::$values, $value);
		
		if ($model != NULL && $model instanceof ActiveRecord) {
			$this->model = $model;
		}
	}
	
	/**
	 * @param $key
	 */
	public function setKey($key)
	{
		$this->localKey = $key;
	}
	
	/**
	 * @return array|Collection
	 * @throws \Exception
	 */
	public function getAll()
	{
		return $this->model::find()
			->whereIn($this->localKey, static::$values)
			->all();
	}
}